package com.cskaoyan.service;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.MarketKeywordExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Since 2022/12/29 10:50
 * @Author LA
 * @Description: KeywordService 实现类
 */
@Service
public class KeywordServiceImp implements KeywordService {

    @Autowired
    MarketKeywordMapper keywordMapper;


    @Override
    public CommonData<MarketKeyword> list(BasePageInfo info, String keyword, String url) {
        // 开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        //
        MarketKeywordExample example = new MarketKeywordExample();
        // 排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        //
        MarketKeywordExample.Criteria criteria = example.createCriteria();

        // 查询逻辑存在,表中deleted字段值为1是删除状态
        criteria.andDeletedEqualTo(false);
        // keyword 判空
        if (StringUtil.isEmpty(keyword)) {
            // 空的时候默认匹配全部了
            criteria.andKeywordLike("%" + "" + "%");
        } else {
            // 不空则会按条件模糊查询
            criteria.andKeywordLike("%" + keyword + "%");
        }
        // url 判空
        if (StringUtil.isEmpty(url)) {
            // 空的时候默认匹配全部了
            criteria.andUrlLike("%" + "" + "%");
        } else {
            // 不空则会按条件模糊查询
            criteria.andUrlLike("%" + url + "%");
        }

        // 调用mapper中的方法返回 相应类型的数据
        List<MarketKeyword> list = keywordMapper.selectByExample(example);
        // 获取分页信息
        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(list);
        // 填充data返回
        return CommonData.data(pageInfo);
    }

    @Override
    public void create(MarketKeyword marketKeyword) {
        keywordMapper.insertSelective(marketKeyword);

    }

    @Override
    public void delete(MarketKeyword keyword) {
        // 获得 要删除的问题的id
        Integer id = keyword.getId();
        // 获得 要删除的问题的逻辑删除标志
        Boolean deleted = keyword.getDeleted();
        // 通过id 更新 删除标记
        MarketKeyword keywordDelete = new MarketKeyword();
        keywordDelete.setId(id);
        // 删除标记取反
        keywordDelete.setDeleted(!deleted);
        keywordMapper.updateByPrimaryKeySelective(keywordDelete);
    }

    @Override
    public MarketKeyword update(MarketKeyword keyword) {
        // 获取要更新的数据的 id
        Integer id = keyword.getId();
        // 创建新数据对象,并填新值
        MarketKeyword keywordUpdate = new MarketKeyword();
        keywordUpdate.setId(id);
        // 更新,更新时间
        keywordUpdate.setUpdateTime(new Date());
        // 更新,关键词,url,是否推荐,是否默认
        keywordUpdate.setKeyword(keyword.getKeyword());
        keywordUpdate.setUrl(keyword.getUrl());
        keywordUpdate.setIsHot(keyword.getIsHot());
        keywordUpdate.setIsDefault(keyword.getIsDefault());

        keywordMapper.updateByPrimaryKeySelective(keywordUpdate);

        return keywordUpdate;
    }
}
